﻿/*   
    Copyright (C) 2009 Galaktika Corporation ZAO

    This file is a part of Galaktika.BusinessMonitor
 
    Galaktika.BusinessMonitor is a free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
      
    You should have received a copy of the GNU General Public License
    along with Galaktika.BusinessMonitor.  If not, see <http://www.gnu.org/licenses/>.
  
    If GPL v.3 is not suitable for your products or company,
    Galaktika Corp provides Galaktika.BusinessMonitor under a flexible commercial license
    designed to meet your specific usage and distribution requirements.
    If you have already obtained a commercial license from Galaktika Corp,
    you can use this file under those license terms.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using Galaktika.BI.Silverlight;

namespace Galaktika.BI.Silverlight.Services
{
    [XmlRoot(Namespace=Common.Namespace)]
    [DataContract(Namespace = Common.Namespace)]
    public class InvokeResultSchema
    {
        public static readonly InvokeResultSchema Empty = new InvokeResultSchema("__none");

        public InvokeResultSchema()
        {
        }

        public InvokeResultSchema(string commandId)
        {
            if (string.IsNullOrEmpty(commandId))
            {
                throw new ArgumentNullException("commandId");
            }

            this.CommandId = commandId;
        }

        public InvokeResultSchema(string commandId, string content)
        {
            if (string.IsNullOrEmpty(commandId))
            {
                throw new ArgumentNullException("commandId");
            }
            if (string.IsNullOrEmpty(content))
            {
                throw new ArgumentNullException("content");
            }

            this.CommandId = commandId;
            this.Content = content;
        }

        public InvokeResultSchema(string commandId, string content, string contentType)
        {
            if (string.IsNullOrEmpty(commandId))
            {
                throw new ArgumentNullException("commandId");
            }
            if (string.IsNullOrEmpty(content))
            {
                throw new ArgumentNullException("content");
            }
            if (string.IsNullOrEmpty(contentType))
            {
                throw new ArgumentNullException("contentType");
            }

            this.CommandId = commandId;
            this.Content = content;
            this.ContentType = contentType;
        }

        [XmlElement(Order = 0)]
        [DataMember(Order = 0)]
        public string CommandId { get; set; }

        [XmlElement(IsNullable = true, Order = 1)]
        [DataMember(Order = 1)]
        public string Content { get; set; }

        [XmlElement(Order = 2)]
        [DataMember(Order = 2)]
        public string ContentType { get; set; }

        [XmlElement(Order = 3)]
        [DataMember(Order = 3)]
        public bool IsArchive { get; set; }

        [XmlElement(IsNullable = true, Order = 4)]
        [DataMember(IsRequired = false, Order = 4)]
        public string SessionId { get; set; }

        private QueryStateDescriptorCollection m_Queries = new QueryStateDescriptorCollection();
        [XmlArray("Queries", Order = 5)]
        [XmlArrayItem("QueryStateDescriptor", typeof(QueryStateDescriptor))]
        [DataMember(Order = 5)]
        public QueryStateDescriptorCollection Queries
        {
            get
            {
                return m_Queries;
            }
            set
            {
                m_Queries = value;
            }
        } 
    }

    [XmlRoot("Queries", Namespace = Common.Namespace)]
    [CollectionDataContract(Name = "Queries", Namespace = Common.Namespace)]
    [KnownType(typeof(QueryStateDescriptor))]
    public class QueryStateDescriptorCollection : System.Collections.ObjectModel.Collection<QueryStateDescriptor>
    {
        public QueryStateDescriptorCollection()
        {
        }

        public void AddRange(IEnumerable<QueryStateDescriptor> items)
        {
            foreach (var item in items)
            {
                base.Add(item);
            }
        }
    }

    [XmlRoot(Namespace = Common.Namespace)]
    [DataContract(Namespace = Common.Namespace)]
    public class QueryStateDescriptor
    {
        public QueryStateDescriptor()
        {
        }

        public QueryStateDescriptor(string id, QueryExecutionState state)
        {
            this.Id = id;
            this.State = state;
        }

        [XmlElement(Order = 0)]
        [DataMember(Order = 0)]
        public string Id { get; set; }

        [XmlElement(Order = 1)]
        [DataMember(Order = 1)]
        public QueryExecutionState State { get; set; }
    }

    [DataContract(Namespace = Common.Namespace)]
    public enum QueryExecutionState
    {
        [EnumMember]
        Queued,

        [EnumMember]
        Executing,

        [EnumMember]
        Done,

        [EnumMember]
        Canceling,

        [EnumMember]
        Canceled,

        [EnumMember]
        Lost,

        [EnumMember]
        Error
    }

    public class KnownContentTypes
    {
        public static readonly string RelationalData = "RelationalData";
        public static readonly string MultidimData = "MultidimData";
        public static readonly string Variables = "Variables";
        public static readonly string Empty = "Empty";
        public static readonly string Error = "Error";
        public static readonly string ADOMDError = "ADOMDError";
        public static readonly string String = "String";
    }
}
